package com.yuqiao.read.lambda;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;

public class TestLambda {

    public static void main(String[] args) {
        Trader liuDeHua = new Trader("刘德华", "香港");
        Trader wuJing = new Trader("吴京","北京");
        Trader luoDaYou = new Trader("罗大佑","台湾");
        Trader liZongSheng = new Trader("李宗盛","台湾");
        List<Transaction> transactions = Arrays.asList(
                new Transaction(liZongSheng, 2011, 300),
                new Transaction(liuDeHua, 2012, 1000),
                new Transaction(liuDeHua, 2011, 400),
                new Transaction(wuJing, 2012, 710),
                new Transaction(wuJing, 2012, 700),
                new Transaction(luoDaYou, 2012, 950)
        );

        List<Transaction> transactionListFor2011 = transactions.stream()
                .filter(t -> t.getYear() == 2011)
                .sorted(comparing(Transaction::getValue))
                .collect(toList());


        List<String> citys = transactions.stream().map(transaction -> transaction.getTrader().getCity()).distinct().collect(toList());

        List<Transaction> 台湾 = transactions.stream().filter(transaction -> transaction.getTrader().getCity().equals("台湾")).distinct().sorted(comparing(transaction -> transaction.getTrader().getName())).collect(toList());
        List<Trader> 台湾2 = transactions.stream().map(transaction -> transaction.getTrader()).filter(trader -> trader.getCity().equals("台湾")).distinct().sorted(comparing(trader -> trader.getName())).collect(toList());
        transactions.stream().map(transaction -> transaction.getTrader().getName()).distinct().sorted().collect(Collectors.joining());
        boolean 台湾1 = transactions.stream().anyMatch(transaction -> transaction.getTrader().getCity().equals("台湾"));
        transactions.stream().filter(transaction -> "台湾".equals(transaction.getTrader().getCity())).map(transaction -> transaction.getValue()).forEach(System.out::println);

        transactions.stream().map(transaction -> transaction.getValue()).reduce(Integer:: max);
        transactions.stream().map(transaction -> transaction.getValue()).reduce(Integer:: min);
        transactions.stream().reduce((t1,t2) -> t1.getValue()<t2.getValue()?t1:t2);
        transactions.stream().min(comparing(Transaction::getValue));
        System.out.println("args = [" + 台湾 + "]");
        System.out.println("args = [" + 台湾2 + "]");

    }
}
